package gui;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import java.awt.GridLayout;
import java.util.Date;

import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JSpinner;
import javax.swing.JComboBox;
import javax.swing.JButton;
import javax.swing.SpinnerDateModel;

import fachlogik.Krankenhausverwaltung;
import fachlogik.Krankenschwester;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class KrankenschwesterEditor extends JFrame
{

	private JPanel contentPane;
	private JTextField tfVorname;
	private JTextField tfName;
	private JTextField tfSvnr;
	private JTextField tfId;
	private JButton btnBearbeiten;
	private JButton btnLschen;
	
	private Krankenhausverwaltung verwaltung;
	private Krankenschwester krankenschwester;
	private SpinnerDateModel gebdat = new SpinnerDateModel();
	private JList herkunft;


	/**
	 * Launch the application.
	 */
//	public static void main(String[] args)
//	{
//		EventQueue.invokeLater(new Runnable() {
//			public void run()
//			{
//				try
//				{
//					PatientenEditor frame = new PatientenEditor(new Krankenhausverwaltung(), new Patient("Test", "Name", new Date(100000000000l), "0123456789", "kassa"));
//				} catch (Exception e)
//				{
//					e.printStackTrace();
//				}
//			}
//		});
//	}

	/**
	 * Create the frame.
	 */
	public KrankenschwesterEditor(Krankenhausverwaltung ver, Krankenschwester schwester, JList her)
	{
		verwaltung = ver;
		krankenschwester = schwester;
		herkunft = her;
		
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 278, 203);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(new BorderLayout(0, 0));
		setContentPane(contentPane);
		
		JPanel panel = new JPanel();
		contentPane.add(panel, BorderLayout.NORTH);
		
		JPanel panel_1 = new JPanel();
		panel.add(panel_1);
		panel_1.setLayout(new GridLayout(0, 2, 0, 0));
		
		JLabel lblVorname = new JLabel("Vorname:");
		panel_1.add(lblVorname);
		
		tfVorname = new JTextField();
		panel_1.add(tfVorname);
		tfVorname.setColumns(10);
		tfVorname.setText(krankenschwester.getVorname());
		
		JLabel lblName = new JLabel("Name:");
		panel_1.add(lblName);
		
		tfName = new JTextField();
		panel_1.add(tfName);
		tfName.setColumns(10);
		tfName.setText(krankenschwester.getName());
		
		JLabel lblGebdatum = new JLabel("Gebdatum:");
		panel_1.add(lblGebdatum);
		
		JSpinner spinner = new JSpinner(gebdat);
		spinner.setEditor(new JSpinner.DateEditor(spinner, "dd.MM.yyyy"));
		spinner.setValue(krankenschwester.getGebdatum());
		panel_1.add(spinner);
		
		JLabel lblSvn = new JLabel("SVN:");
		panel_1.add(lblSvn);
		
		tfSvnr = new JTextField();
		panel_1.add(tfSvnr);
		tfSvnr.setColumns(10);
		tfSvnr.setText(krankenschwester.getSvnr());
		
		JLabel lblId = new JLabel("Krankenschwester-ID:");
		panel_1.add(lblId);
		
		tfId = new JTextField();
		panel_1.add(tfId);
		tfId.setColumns(10);
		tfId.setText(krankenschwester.getId());
		
		JPanel panel_2 = new JPanel();
		contentPane.add(panel_2, BorderLayout.CENTER);
		
		btnBearbeiten = new JButton("Bearbeiten");
		btnBearbeiten.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				try
				{
					verwaltung.krankenschwesterhinzu(tfVorname.getText(), tfName.getText(), gebdat.getDate(), tfSvnr.getText(), tfId.getText());
					verwaltung.getKrankenschwesterliste().remove(krankenschwester);
					herkunft.updateUI();
					dispose();
				} catch (Exception e)
				{
					JOptionPane.showMessageDialog(KrankenschwesterEditor.this, e.getMessage());
				}
			}
		});
		panel_2.add(btnBearbeiten);
		
		btnLschen = new JButton("L\u00F6schen");
		btnLschen.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				verwaltung.getKrankenschwesterliste().remove(krankenschwester);
				herkunft.updateUI();												//updatet die JList
				dispose();
			}
		});
		panel_2.add(btnLschen);
		
		this.setVisible(true);
	}
	
	public void refresh(){
		herkunft.updateUI();
		herkunft.validate();
	}

}